package com.fjeld.client.gui.control.navigation.tree;

import com.fjeld.swingx.JTransparentPanel;
import com.fjeld.middleware.jaxb.V1Node;
import com.fjeld.middleware.jaxb.V1ID;
import com.fjeld.client.gui.util.factories.LabelFactory;
import com.fjeld.client.gui.screens.ProductionLineView;
import com.fjeld.client.util.cache.ClientCacheManager;

import javax.swing.*;
import java.awt.*;
import java.util.Observer;
import java.util.Observable;
import java.util.Map;
import java.util.HashMap;

import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: fjelds
 * Date: Mar 12, 2010
 * Time: 11:40:15 PM
 *
 * A one line entry into a tree
 */
public class TreeEntryPanel extends JTransparentPanel implements Observer
{
    private static Image globeImage = new ImageIcon(TreeEntryPanel.class.getResource(
                "/com/explodingpixels/macwidgets/icons/Network.png")).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
    private static Icon greyGlobeIcon = new ImageIcon(globeImage);
    private static final Logger logger = Logger.getLogger(TreeEntryPanel.class);
    private static ClientCacheManager ccm = ClientCacheManager.getInstance();

    private ProductionLineView productionLineView;
    private V1Node node;
    private JPanel children, expansionIconPanel, headerPanel, content;
    private Map<String, TreeEntryPanel> childMap;
    private Box childrenBox;

    public TreeEntryPanel(ProductionLineView productionLineView, V1Node node)
    {
        this.productionLineView = productionLineView;
        this.node = node;
        children = new JTransparentPanel();
        children.setBorder(BorderFactory.createEmptyBorder(0,12,0,0));
        expansionIconPanel = new JTransparentPanel();
        headerPanel = new JTransparentPanel();
        content = new JTransparentPanel();
        childMap = new HashMap<String, TreeEntryPanel>(100);
        childrenBox = Box.createVerticalBox();
        children.add(childrenBox, BorderLayout.NORTH);

        JLabel label = LabelFactory.createTreeLabel(node.getName());
        label.setIcon(greyGlobeIcon);
        label.addMouseListener(new TreeEntryMouseListener(this, node, productionLineView));
        headerPanel.add(label, BorderLayout.NORTH);

        content.add(headerPanel, BorderLayout.NORTH);
        content.add(children, BorderLayout.CENTER);

        add(content, BorderLayout.NORTH);

        node.addObserver(this);
        updateChildren();
    }

    public TreeEntryPanel(ProductionLineView productionLineView, V1ID ref)
    {
        this(productionLineView, ccm.getNode(ref.getNodeID()));
    }

    private void updateChildren()
    {
        // Brute force.
        // Remove all panels.
        for (TreeEntryPanel panel : childMap.values())
        {
            childrenBox.remove(panel);
            panel.tearDown();
        }
        childMap.clear();

        // Add panels of children
        for (V1ID ref : node.getV1ID())
        {
            TreeEntryPanel child = new TreeEntryPanel(productionLineView, ref);
            childrenBox.add(child);
            childMap.put(ref.getNodeID(), child);
        }

        childrenBox.revalidate();
        childrenBox.repaint();
    }

    private void tearDown()
    {
        node.deleteObserver(this);
    }

    public void update(Observable observable, Object o)
    {
        logger.info("Change to data model node [" + node.getNodeID() + "]");
        updateChildren();
    }
}
